package vue;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

import controleur.ControleurToile;

import modele.ModeleToile;


public class VueFenetre extends JFrame implements Runnable
{
	private static final long serialVersionUID = 1L;
	private JScrollPane jsp;
	private JButton modifBtn;
	private JButton undo;
	private JButton redo;
	private JMenuItem save;
	private JMenuItem cancel;
	private JMenuItem back;
	private JButton sauvegarder;

	public void run()
	{
		this.setTitle("KCK UML");
	    this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
	    
		
	    /* La Toile sur laquelle on dessiner */
		ModeleToile modeleToile = new ModeleToile();
		VueToile toile = new VueToile(modeleToile);
		toile.setPreferredSize(new Dimension(1000, 600));
		
		/* Barre d'outils */
		ControleurToile controleur = new ControleurToile(toile, this);
		
		JToolBar outils = new JToolBar();
		
		JButton classe = new JButton("Classe", new ImageIcon("Images/add.png"));
		classe.setActionCommand("NewClasse");
		classe.addActionListener(controleur);
		
		JButton association = new JButton("Association", new ImageIcon("Images/add.png"));
		association.setActionCommand("NewAssociation");
		association.addActionListener(controleur);
		
		JButton composition = new JButton("Composition", new ImageIcon("Images/add.png"));
		composition.setActionCommand("NewComposition");
		composition.addActionListener(controleur);
		
		undo = new JButton(new ImageIcon("Images/undo.png"));
		undo.setToolTipText("Annuler");
		undo.setActionCommand("AnnulerOperation");
		undo.addActionListener(controleur);
		
		redo = new JButton(new ImageIcon("Images/redo.png"));
		redo.setToolTipText("Rétablir");
		redo.setActionCommand("RefaireOperation");
		redo.addActionListener(controleur);
		
		JButton sauvegarderSous = new JButton(new ImageIcon("Images/sauvegarder_sous.png"));
		sauvegarderSous.setToolTipText("Enregistrer sous ...");
		sauvegarderSous.setActionCommand("Enregistrer sous ...");
		sauvegarderSous.addActionListener(controleur);
		
		sauvegarder = new JButton(new ImageIcon("Images/sauvegarder.png"));
		sauvegarder.setToolTipText("Enregistrer");
		sauvegarder.setActionCommand("Enregistrer");
		sauvegarder.addActionListener(controleur);
		
		JButton generer = new JButton(new ImageIcon("Images/generer-code.png"));
		generer.setToolTipText("Génerer le code");
		generer.setActionCommand("GenererCode");
		generer.addActionListener(controleur);
		
		outils.add(sauvegarderSous);
		outils.add(sauvegarder);
		outils.addSeparator();
		outils.add(classe);
		outils.add(association);
		outils.add(composition);
		outils.addSeparator();
		outils.add(undo);
		outils.add(redo);
		outils.addSeparator();
		outils.add(generer);
		
		
		/* Barre des menus */
		JMenuBar jmb = new JMenuBar();

		JMenu file = new JMenu("Fichier");
		//pour alt, puis f
		file.setMnemonic(KeyEvent.VK_F);
		
		JMenuItem neww = new JMenuItem("Nouveau", new ImageIcon("Images/nouveau.png")); 
		//pour le ctrl-n
		neww.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));
		neww.addActionListener(controleur); 
		file.add(neww);
		
		JMenuItem open = new JMenuItem("Ouvrir", new ImageIcon("Images/ouvrir.png"));
		//pour le ctrl-o
		open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
		open.addActionListener(controleur); 
		file.add(open);
		
		save = new JMenuItem("Enregistrer", new ImageIcon("Images/sauvegarder.png"));
		save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
		save.addActionListener(controleur);
		file.add(save);
		
		JMenuItem save_as = new JMenuItem("Enregistrer sous ...", new ImageIcon("Images/sauvegarder_sous.png"));
		save_as.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, Event.CTRL_MASK));
		save_as.addActionListener(controleur);
		file.add(save_as);
		
		JMenuItem exit = new JMenuItem("Quitter", new ImageIcon("Images/exit-32.png"));  
		exit.setActionCommand("Quitter");
		exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));
		exit.addActionListener(controleur);
		file.add(exit);

		
		JMenu edit = new JMenu("Edition");
		//pour alt, puis e
		edit.setMnemonic(KeyEvent.VK_E);
		
		cancel = new JMenuItem("Annuler", new ImageIcon("Images/undo.png"));
		//pour le ctrl-z
		cancel.setActionCommand("AnnulerOperation");
		cancel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK));
		cancel.addActionListener(controleur);
		edit.add(cancel);
		
		back = new JMenuItem("Rétablir", new ImageIcon("Images/redo.png"));
		//pour le ctrl-y
		back.setActionCommand("RefaireOperation");
		back.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK));
		back.addActionListener(controleur);
		edit.add(back);
		
		/*JMenuItem copy = new JMenuItem("Copier");
		edit.add(copy);
		JMenuItem cut = new JMenuItem("Couper");
		edit.add(cut);
		JMenuItem paste = new JMenuItem("Coller");
		edit.add(paste);*/
		
		JMenu source = new JMenu("Source");
		//pour alt, puis s
		source.setMnemonic(KeyEvent.VK_S);
		
		JMenuItem generate = new JMenuItem("Génerer le code", new ImageIcon("Images/generer-code.png"));
		//pour le ctrl-g
		generate.setActionCommand("GenererCode");
		generate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, Event.CTRL_MASK));
		generate.addActionListener(controleur);
		source.add(generate);
		
		
		setUndoBtnEnabled(false);
		setRedoBtnEnabled(false);
		setSauvegarderBtnEnabled(false);
		
		
		jmb.add(file);
		jmb.add(edit);
		jmb.add(source);
		
		/* Réglages de la fenêtre */
		this.setLayout(new BorderLayout());
		
		jsp = new JScrollPane(toile);
		jsp.setAutoscrolls(true);
		this.add(jsp, BorderLayout.SOUTH);
		this.add(outils, BorderLayout.NORTH);
		
		this.setJMenuBar(jmb);
		this.pack();
		this.setVisible(true);
	}
	
	public void redimmensionner() {
		//jsp.scrollRectToVisible(new Rectangle(w,h));
	}
	
	public JScrollPane getJsp() {
		return jsp;
	}

	public void setJsp(JScrollPane jsp) {
		this.jsp = jsp;
	}
	
	public JButton getModifBtn() {
		return modifBtn;
	}

	public void setModifBtn(JButton modifBtn) {
		this.modifBtn = modifBtn;
	}
	
	public void setUndoBtnEnabled(boolean b) {
		this.undo.setEnabled(b);
		this.cancel.setEnabled(b);
	}
	
	public void setRedoBtnEnabled(boolean b) {
		this.redo.setEnabled(b);
		this.back.setEnabled(b);
	}
	
	public void setSauvegarderBtnEnabled(boolean b) {
		this.sauvegarder.setEnabled(b);
		this.save.setEnabled(b);
	}
	
}
